﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Crm.Views;
using Crm.CrmClient;
using DevExpress.Xpf.NavBar;

namespace Crm
{
    public partial class MainPage : Page
    {
        CrmClient.CrmServiceClient client = new CrmClient.CrmServiceClient();
        public bool AdayCari { get; set; }
        public string CariKodu { get; set; }
        public string CariUnvani { get; set; }
        public int CariID { get; set; }
        private Rapor AktifRapor = null;

        public MainPage()
        {
            InitializeComponent();
            client.GorusmeyeBaslaCompleted += client_GorusmeyeBaslaCompleted;
            client.RaporListeleCompleted += client_RaporListeleCompleted;
            client.GetSqlQueryCompleted += client_GetSqlQueryCompleted;
            this.Loaded += MainPage_Loaded;
        }

        void client_GetSqlQueryCompleted(object sender, GetSqlQueryCompletedEventArgs e)
        {
            if (e.Error == null && e.Result != null)
            {
                RaporView rpr = new RaporView();
                rpr.Title = AktifRapor.Adi;
                rpr.GridData = e.Result[0];
                rpr.GrafikData = e.Result[1];
                rpr.GrafikTipi = AktifRapor.ChartTipi;
                rpr.GrafikAlan = AktifRapor.ChartAlan;
                rpr.GrafikDeger = AktifRapor.ChartDeger;
                rpr.RaporTipi = AktifRapor.Tipi;
                rpr.Show();
            }
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            client.RaporListeleAsync();
        }

        void client_RaporListeleCompleted(object sender, RaporListeleCompletedEventArgs e)
        {
            if (e.Error == null && e.Result != null)
            {
                var item = navBar.Groups[5];
                item.DisplaySource = DisplaySource.Content;
                StackPanel stackPanel = new StackPanel();
                item.Content = stackPanel;

                foreach (var b in e.Result)
                {
                    HyperlinkButton btn = new HyperlinkButton();
                    btn.Content = b.Adi;
                    btn.Tag = b;
                    btn.TargetName = "ContentFrame";
                    btn.Style = (Style)btn.TryFindResource("LinkStyle");
                    btn.Click += btn_Click;
                    stackPanel.Children.Add(btn);
                }
            }
        }

        void btn_Click(object sender, RoutedEventArgs e)
        {
            Rapor rp = (Rapor)((HyperlinkButton)sender).Tag;
            if (rp.GridSorgu.ToLower().IndexOf("where") > -1)
            {
                RaporParametre prm = new RaporParametre();
                prm.Rapor = new Rapor() { Adi = rp.Adi, ChartAlan = rp.ChartAlan, ChartDeger = rp.ChartDeger, ChartSorgu = rp.ChartSorgu, ChartTipi = rp.ChartTipi, GridSorgu = rp.GridSorgu, ID = rp.ID, Tipi = rp.Tipi };
                prm.Show();
                prm.Closed += prm_Closed;
            }
            else
            {
                RaporCalistir(rp);
            }
        }

        private void RaporCalistir(Rapor rp)
        {
            AktifRapor = rp;
            string sql1 = (rp.ChartSorgu == "0") ? "" : rp.ChartSorgu;
            client.GetSqlQueryAsync(rp.GridSorgu, sql1);
        }

        void prm_Closed(object sender, EventArgs e)
        {
            RaporParametre lw = (RaporParametre)sender;
            if (lw.DialogResult == true)
            {
                RaporCalistir(lw.Rapor);
            }
        }

        void client_GorusmeyeBaslaCompleted(object sender, CrmClient.GorusmeyeBaslaCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                this.LayoutRoot.RowDefinitions.Clear();
                this.LayoutRoot.ColumnDefinitions.Clear();
                this.LayoutRoot.Children.Clear();
                this.LayoutRoot.Children.Add(new DashBoard(AdayCari, CariKodu, CariUnvani, CariID, e.Result));
            }
        }

        // After the Frame navigates, ensure the HyperlinkButton representing the current page is selected
        private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
        {
            //foreach (UIElement child in LinksStackPanel.Children)
            //{
            //    HyperlinkButton hb = child as HyperlinkButton;
            //    if (hb != null && hb.NavigateUri != null)
            //    {
            //        if (hb.NavigateUri.ToString().Equals(e.Uri.ToString()))
            //        {
            //            VisualStateManager.GoToState(hb, "ActiveLink", true);
            //        }
            //        else
            //        {
            //            VisualStateManager.GoToState(hb, "InactiveLink", true);
            //        }
            //    }
            //}
        }

        // If an error occurs during navigation, show an error window
        private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            e.Handled = true;
            ChildWindow errorWin = new ErrorWindow(e.Uri);
            errorWin.Show();
        }

        private void lnkYeniGorusme_Click_1(object sender, RoutedEventArgs e)
        {
            Gorusme grs = new Gorusme();
            grs.GorusmeEvragi = true;
            grs.EskiEvrak = false;
            grs.Show();
        }

        private void lnkYeniRandevu_Click_1(object sender, RoutedEventArgs e)
        {
            Gorusme grs = new Gorusme();
            grs.GorusmeEvragi = false;
            grs.EskiEvrak = false;
            grs.Show();
        }

        private void lnkAdayCari_Click_1(object sender, RoutedEventArgs e)
        {
            AdayCariKarti frm = new AdayCariKarti();
            frm.EskiEvrak = false;
            frm.CariKodu = "";
            frm.Show();
        }

        private void lnkCariSec_Click_1(object sender, RoutedEventArgs e)
        {
            CariFiltre frm = new CariFiltre();
            frm.Closed += frm_Closed;
            frm.Show();
        }

        void frm_Closed(object sender, EventArgs e)
        {
            CariFiltre fl = (CariFiltre)sender;
            if (fl.DialogResult.Value)
            {
                Kullanici usr = Session.Instance.GetValue<Kullanici>("user");
                AdayCari = fl.AdayCari;
                CariKodu = fl.Kodu;
                CariUnvani = fl.Unvani;
                CariID = fl.ID;
                client.GorusmeyeBaslaAsync(AdayCari, CariID, CariKodu, CariUnvani, usr.ID);
            }
        }

        private void lnkParametre_Click_1(object sender, RoutedEventArgs e)
        {
            Parametreler prm = new Parametreler();
            prm.Show();
        }

        private void lnkKullaniciParametre_Click_1(object sender, RoutedEventArgs e)
        {
            KullaniciParametreleri prm = new KullaniciParametreleri();
            prm.Show();
        }

        private void lnkRaporTanim_Click_1(object sender, RoutedEventArgs e)
        {
            RaporYap frm = new RaporYap();
            frm.Show();
        }

        private void lnktest_Click_1(object sender, RoutedEventArgs e)
        {

        }


        private void lnkTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ContentFrame.Navigate(new Uri("/Home", UriKind.Relative));
        }
    }
}
